package com.cn.kehong.service;

import com.cn.kehong.api.IPlanYearDynamicService;
import com.cn.kehong.api.ISysItemService;
import com.cn.kehong.dao.PlanYearDynamicMapper;
import com.cn.kehong.domain.PlanYearDynamic;
import com.cn.kehong.domain.PlanYearDynamicExample;
import com.cn.kehong.domain.SysItem;
import com.cn.kehong.utils.CollectionsUtil;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class PlanYearDynamicServiceImpl implements IPlanYearDynamicService {

    @Autowired
    private PlanYearDynamicMapper planYearDynamicMapper;

    @Autowired
    private ISysItemService sysItemService;

    @Override
    public List<PlanYearDynamic> getPlanYearDynamics(String year) throws Exception {
        PlanYearDynamicExample example = new PlanYearDynamicExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andPlanYearEqualTo(year);
        example.setOrderByClause("sort_index asc");
        List<PlanYearDynamic> planYearDynamics = planYearDynamicMapper.selectByExample(example);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
        String currentYear = simpleDateFormat.format(new Date());
        if(CollectionsUtil.isEmpty(planYearDynamics)){
            planYearDynamics = Lists.newLinkedList();
            List<SysItem> sysItems = sysItemService.getMgItemByType("10");
            PlanYearDynamic header = new PlanYearDynamic();
            header.setTabName(currentYear+"年");
            header.setSortIndex(1);
            header.setPlanYear(year);
            for(int i= 1;i<=12;i++){
                Class<PlanYearDynamic> cls = PlanYearDynamic.class;
                Method method = cls.getMethod("setV" + i, String.class);
                method.invoke(header, i+"月");
            }
            planYearDynamics.add(header);
            int index = 2;
            for(SysItem sysItem:sysItems){
                String itemName = sysItem.getItemName();
                PlanYearDynamic gradeItem = new PlanYearDynamic();
                gradeItem.setTabName(itemName);
                for(int i= 1;i<=12;i++){
                    Class<PlanYearDynamic> cls = PlanYearDynamic.class;
                    Method method = cls.getMethod("setV" + i, String.class);
                    method.invoke(gradeItem, "0.0");
                }
                gradeItem.setSortIndex(index);
                gradeItem.setPlanYear(year);
                planYearDynamics.add(gradeItem);
                ++index;
            }

            PlanYearDynamic hejiVo = this.setPlanYearDynamic("计划合计",year);
            hejiVo.setSortIndex(index);
            ++index;
            PlanYearDynamic statusVo = this.setPlanYearDynamic("计划状态",year);
            statusVo.setSortIndex(index);
            ++index;
            PlanYearDynamic yearVo = this.setPlanYearDynamic("实际达成",year);
            yearVo.setSortIndex(index);
            ++index;
            PlanYearDynamic pianchaVo = this.setPlanYearDynamic("欠缺数量",year);
            pianchaVo.setSortIndex(index);
            ++index;
            PlanYearDynamic pianchaPerVo = this.setPlanYearDynamic("达成率%",year);
            pianchaPerVo.setSortIndex(index);
            ++index;
            planYearDynamics.add(hejiVo);
            planYearDynamics.add(statusVo);
            planYearDynamics.add(yearVo);
            planYearDynamics.add(pianchaVo);
            planYearDynamics.add(pianchaPerVo);
        }
        return planYearDynamics;
    }

    private PlanYearDynamic setPlanYearDynamic(String name,String year) throws Exception {
        PlanYearDynamic planMothVo = new PlanYearDynamic();
        planMothVo.setTabName(name);
        planMothVo.setPlanYear(year);
        for (int i = 1; i <= 12; i++) {
            Class<PlanYearDynamic> cls = PlanYearDynamic.class;
            Method method = cls.getMethod("setV" + i, String.class);
            method.invoke(planMothVo, "");
        }
        return planMothVo;
    }


    @Override
    public String updateBatchPlanYearDynamics(List<PlanYearDynamic> planYearDynamics) throws Exception {
        if(CollectionsUtil.isEmpty(planYearDynamics)){
            return "数组不能为空";
        }
        String planYear = planYearDynamics.get(0).getPlanYear();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
        String currentYear = simpleDateFormat.format(new Date());
        if(!StringUtils.equals(planYear,currentYear)){
            return "只能修改当前年度";
        }
        PlanYearDynamicExample example = new PlanYearDynamicExample();
        example.createCriteria().andPlanYearEqualTo(planYear);
        int count = planYearDynamicMapper.deleteByExample(example);
        if(count < 0){
            return "更新表异常！";
        }
        for(PlanYearDynamic planYearDynamic:planYearDynamics){
            planYearDynamic.setId(CommonUtil.generateRandomNum("dynamic-"));
        }

        count = planYearDynamicMapper.batchInsertPlanYearDynamics(planYearDynamics);
        return CommonUtil.outStr(count);
    }
}
